package org.mxp.demo10;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.mxp.utils.RabbitMQUtils;

import java.util.HashMap;
import java.util.Map;

public class SendObj {
    public static void main(String[] args) throws Exception {
        // 交换机名称
        String EXCHANGE_NAME = "test_exchange";
        // 队列的名称
        String QUEUE_NAME = "test_queue";

        // 获取信道
        Channel channel = RabbitMQUtils.getChannel();
        // 声明交换机，第一个参数为交换机名称，第二个参数为交换机类型
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        // 构建队列参数
        Map<String,Object> arguments = new HashMap<>();
        // 设置队列元素最大优先级未为10，最大255
        arguments.put("x-max-priority", 10);
        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, arguments).getQueue();
        // 绑定交换机与队列
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "test");

        // 定义消息内容
        String message = "hello world ";
        // 发送10条消息
        for (int i = 0; i < 10; i++) {
            if (i % 3 == 0) {
                // 设置当前消息优先级
                AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().priority(i).build();
                channel.basicPublish(EXCHANGE_NAME, "test", properties, (message + i).getBytes());
            } else {
                channel.basicPublish(EXCHANGE_NAME, "test", null, (message + i).getBytes());
            }
        }
        System.out.println("消息发送完毕");
    }
}
